package com.yooim.chat;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ChatRoom
 */
public class ChatRoom extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private static ChatBot bot = null;

	   public ChatRoom() {
	        super();
	        bot = new ChatBot();
	        bot.go();
	        bot.startNew();
	   }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	     if (request.getParameter("target") != null 
	                && request.getParameter("message") != null){
	            
	            bot.sendMessage(request.getParameter("target").toString(), 
	                           request.getParameter("message").toString());
	            
	        }

	     if (request.getParameter("getMessages") != null){
	            response.getWriter().print(
	               bot.getMessages(
	                    request.getParameter("getMessages").toString()));
	        }
	     
	     if (request.getParameter("addToRoster") != null){
	            bot.addToRoster(
	                 request.getParameter("addToRoster").toString(), 
	                 request.getParameter("rosterName").toString());

	        }
	     
	     if (request.getParameter("getRoster") != null){
	            response.getWriter().println(bot.getRoster());
	        }

	     if (request.getParameter("removeFromRoster") != null){
	            bot.removeFromRoster(
	               request.getParameter("removeFromRoster").toString());
	        }


	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
